<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class AdjudicationsController extends AppController {

    var $name = 'Adjudications';

    function index() {
        $this->layout = "ajax";
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);
        if ($proyect_id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        } elseif (($this->Session->read('fase1') == 1) OR ($this->Session->read('fase1') == 2)) {
            $this->Adjudication->recursive = -1;
            $this->set('adjudicaciones', $this->Adjudication->find('all', array('conditions' => array('Adjudication.proyect_id' => $proyect_id), 'fields' => array('Adjudication.id', 'Adjudication.proyect_id', 'Adjudication.fecha_real', 'Adjudication.tipo', 'Adjudication.observacion', 'Adjudication.fecha_programada'), 'order' => array('Adjudication.fecha_programada ASC'))));
        } else {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function add() {
        $this->layout = "ajax";
        if (!empty($this->data)) {
            if ($this->Adjudication->save($this->data)) {
                $this->Session->setFlash('Registro guardado correctamente');
                $this->redirect(array('Controller' => 'Adjudication', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error guardando los datos.');
            }
        } else {
            $proyect_id = $this->Session->read('proy_id');
            $this->data['Adjudication']['proyect_id'] = $proyect_id;
//            $this->Adjudication->recursive = -1;
//            $this->data = $this->Adjudication->find('first', array('conditions' => array('Adjudication.id' => $id), 'fields' => array('Adjudication.id', 'Adjudication.fecha_real', 'Adjudication.tipo', 'Adjudication.observacion', 'Adjudication.fecha_programada', 'Adjudication.proyect_id', 'Adjudication.user_id'), 'order' => array('Adjudication.fecha_programada ASC')));
        }
    }

    function edit($id) {
        $this->layout = "ajax";
        if (!empty($this->data)) {
            if ($this->Adjudication->save($this->data)) {
                $this->Session->setFlash('Registro guardado correctamente');
                $this->redirect(array('Controller' => 'Adjudication', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error guardando los datos.');
            }
        } else {
            $this->Adjudication->recursive = -1;
            $this->data = $this->Adjudication->find('first', array('conditions' => array('Adjudication.id' => $id), 'fields' => array('Adjudication.id', 'Adjudication.fecha_real', 'Adjudication.tipo', 'Adjudication.observacion', 'Adjudication.fecha_programada', 'Adjudication.proyect_id', 'Adjudication.user_id'), 'order' => array('Adjudication.fecha_programada ASC')));
        }
    }

    function delete($id) {
        if ($this->Adjudication->delete($id)) {
            $this->Session->setFlash('Registro borrado correctamente');
            $this->redirect(array('controller' => 'Adjudications', 'action' => 'index'));
        } else {
            $this->Session->setFlash('Error borrando los datos, intentelo nuevamente.');
        }
    }

}

?>
